/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.surfmanager.util;

import com.surfmanager.model.Profile;
import com.surfmanager.model.SurfSession;
import com.surfmanager.model.SurfSessionFilter;
import com.surfmanager.model.Trip;
import com.surfmanager.model.TripFilter;
import com.surfmanager.model.result.ProfileResult;
import com.surfmanager.model.result.SurfSessionResult;
import com.surfmanager.model.result.TripResult;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Query;

/**
 *
 * @author eamiker
 */
public class Utilities {
    
    
     public static Date getDateFromString(String dateString){
    
       try {  
           if(dateString!=null){
     SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        return formatter.parse(dateString);
           }
       } catch (ParseException ex) {
            Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
       
    }
     
     public static ProfileResult convertProfile(Profile profile) {
      
        ProfileResult result=null;
        if(profile!=null){
         result = new ProfileResult();
        result.setAge(profile.getAge());
        result.setCreated(profile.getCreated().toString());
        result.setEmail(profile.getEmail());
        result.setFirstName(profile.getFirstName());
        result.setId(profile.getId());
        result.setLastName(profile.getLastName());
        result.setNoteOrganizer(profile.getNoteOrganizer());
        result.setNoteParticipant(profile.getNoteParticipant());
        result.setOrganizations(profile.getOrganizations());
        result.setParticipations(profile.getParticipations());
        result.setPassword(profile.getPassword());
        result.setPhone(profile.getPhone());
        result.setUserName(profile.getUserName());
        result.setSessions(convertSessions(profile.getSessions()));
        }
        return result;
    }

    public static Set<SurfSessionResult> convertSessions(Set<SurfSession> sessions) {
    
        Set<SurfSessionResult> sessionsResult = new TreeSet<SurfSessionResult>();
        for (SurfSession surfSession : sessions) {
            sessionsResult.add(convertSession(surfSession));
        }
    return sessionsResult;
    }

    public static SurfSessionResult convertSession(SurfSession surfSession) {
    
        SurfSessionResult result =null;
        if(surfSession!=null){
        result = new SurfSessionResult();
        result.setDepartDate(surfSession.getDepartDate().toString());
        result.setDepartPlace(surfSession.getDepartPlace());
        result.setDescription(surfSession.getDescription());
        result.setMaxParticipants(surfSession.getMaxParticipants());
        result.setPricePerParticipant(surfSession.getPricePerParticipant());
        if(surfSession.getProfile()!=null){
        result.setProfile(surfSession.getProfile().getUserName());
        }
        result.setRetourDate(surfSession.getRetourDate().toString());
        result.setSessionDuration(surfSession.getSessionDuration());
        result.setSessionId(surfSession.getSessionId());
        result.setSessionPlace(surfSession.getSessionPlace());
        result.setSessionProbability(surfSession.getSessionProbability());
        result.setTransport(surfSession.getTransport());
        }
        
        return result;
        
    }

    public static Query populateParameters(Query query, SurfSessionFilter filter) {
   
        if(filter.getUserName()!=null){
            query.setParameter("userName", filter.getUserName());
        } 
        if(filter.getDepartDate()!=null){
            query.setParameter("departDate",filter.getDepartDate());
        }
        if(filter.getRetourDate()!=null){
            query.setParameter("retourDate",filter.getRetourDate());
        }
         if(filter.getMaxParticipants() !=null){
            query.setParameter("maxParticipants",filter.getMaxParticipants() );
        }
          if(filter.getPricePerParticipant() !=null){
            query.setParameter("pricePerParticipant",filter.getPricePerParticipant() );
        }
         if(filter.getDepartPlace() !=null){
            query.setParameter("departPlace",filter.getDepartPlace());
        }
          if(filter.getSessionDuration() !=null){
            query.setParameter("sessionDuration",filter.getSessionDuration());
        }
           if(filter.getSessionPlace() !=null){
            query.setParameter("sessionPlace",filter.getSessionPlace());
        }
            if(filter.getSessionProbability() !=null){
            query.setParameter("sessionProbability",filter.getSessionProbability());
        }
             if(filter.getTransport() !=null){
            query.setParameter("transport",filter.getTransport());
        }
        
        return query;    
    }
    
    
     public static Query populateParameters(Query query, TripFilter filter) {
   
        if(filter.getUserName()!=null){
            query.setParameter("userName", filter.getUserName());
        } 
        if(filter.getDepartDate()!=null){
            query.setParameter("departDate",filter.getDepartDate());
        }
        if(filter.getRetourDate()!=null){
            query.setParameter("retourDate",filter.getRetourDate());
        }
         if(filter.getMaxParticipants() !=null){
            query.setParameter("maxParticipants",filter.getMaxParticipants() );
        }
          if(filter.getPricePerParticipant() !=null){
            query.setParameter("pricePerParticipant",filter.getPricePerParticipant() );
        }
         if(filter.getDepartPlace() !=null){
            query.setParameter("departPlace",filter.getDepartPlace());
        }
          if(filter.getTripDuration() !=null){
            query.setParameter("tripDuration",filter.getTripDuration());
        }
           if(filter.getTripPlace() !=null){
            query.setParameter("tripPlace",filter.getTripPlace());
        }
            if(filter.getTripProbability() !=null){
            query.setParameter("tripProbability",filter.getTripProbability());
        }
             if(filter.getTransport() !=null){
            query.setParameter("transport",filter.getTransport());
        }
        
        return query;
        
        
    }

    public static String getQueryWithoutSuffix(String queryPattern) {
        String result ="";
        String suffix = queryPattern.substring(queryPattern.length()-5);
        if(suffix.contains("AND")){
            result = queryPattern.substring(0, queryPattern.length()-4);
        }else if(suffix.contains("where")){
            result = queryPattern.substring(0, queryPattern.length()-6);
        }
            
        return result;
    
    }

    
     
    public static Set<TripResult> convertTrips(Set<Trip> trips) {
    
        Set<TripResult> tripsResult = new TreeSet<TripResult>();
        for (Trip trip : trips) {
            tripsResult.add(convertTrip(trip));
        }
    return tripsResult;
    }

    public static TripResult convertTrip(Trip trip) {
    
        TripResult result =null;
        if(trip!=null){
        result = new TripResult();
        result.setDepartDate(trip.getDepartDate().toString());
        result.setDepartPlace(trip.getDepartPlace());
        result.setDescription(trip.getDescription());
        result.setMaxParticipants(trip.getMaxParticipants());
        result.setPricePerParticipant(trip.getPricePerParticipant());
        if(trip.getProfile()!=null){
        result.setProfile(trip.getProfile().getUserName());
        }
        result.setRetourDate(trip.getRetourDate().toString());
        result.setTripDuration(trip.getTripDuration());
        result.setTripId(trip.getTripId());
        result.setTripPlace(trip.getTripPlace());
        result.setTripProbability(trip.getTripProbability());
        result.setTransport(trip.getTransport());
        }
        
        return result;
        
    }
     
}
